<template>
  <!-- 充值查询 -->
  <div class="user_recharge_query">
    <el-form :inline="true" :model="elform" class="demo-form-inline1">
      <el-form-item label="充值类型:">
        <el-select
          v-model="adjustTypeId"
          placeholder="请选择"
          size="mini"
          style="width: 120px"
          clearable
        >
          <el-option
            v-for="item in deductionlist"
            :key="item.pkTypeId"
            :label="item.typeName"
            :value="item.pkTypeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否领取:">
        <el-select
          v-model="adjustState"
          placeholder="请选择"
          size="mini"
          clearable
          style="width: 120px"
        >
          <el-option
            v-for="item in adjustStateOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="gosearch()" size="mini">查询</el-button>
        <el-button type="primary" @click="exportInfo()" size="mini">导出</el-button>
        <el-button type="primary" @click="exportInfo('print')" size="mini">打印</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableinformation"
      style="width: 100%"
      border="border"
      :header-cell-style="headFirst"
      :cell-style="
        tableinformation.length ? (res) => headFirst_col(res, tableinformation) : () => {}
      "
      highlight-current-row
      stripe
      v-loading="loading"
    >
      <el-table-column prop="adjustTypeName" label="充值类型名称" min-width="120px"></el-table-column>
      <el-table-column prop="sumRechargeAmount" label="充值金额(元)">
        <!-- <template slot-scope="scope">
          <div>{{ scope.row.sumRechargeAmount | amountyuan }}</div>
        </template>-->
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},

      deductionlist: [],
      rules: {},
      // 表格加载中
      loading: false,
      // 当前登录号的id
      administrator: '',
      // 单位id
      changeschoolid: '',
      // 合计数据
      detailsTotal: null,
      // 导出数据
      agentlistout: [],
      // 充值数据
      tableinformation: [],
      /**搜索条件 */
      // 状态
      adjustState: '',
      // 充值类型
      adjustTypeId: '',
      // 时间
      value1: '',
      value2: '',
      datetime: '',
      time: '',
      adjustStateOption: [
        {
          value: 0,
          label: '未领取'
        },
        {
          value: 1,
          label: '已领取'
        }
      ],
      searchInfo: {}
    }
  },
  created() {
    this.changeschoolid = this.$store.state.user.changeschoolid
    this.administrator = sessionStorage.getItem('administrator')
    // 账号类型
    this.userType = sessionStorage.getItem('userType')
    // 初始化时间
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
    // 获取无卡充值类型
    this.getConType()
  },
  methods: {
    // 获取无卡充值类型
    getConType() {
      this.$http({
        url: this.$http.adornUrl('/unit/recharge-type-list'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: this.changeschoolid,
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.deductionlist = data.messageContent
        } else {
          this.$message({
            showClose: true,
            message: data.errorMessage,
            type: 'error'
          })
        }
      })
    },
    //查询
    async gosearch() {
      this.pageNum = 1
      this.searchInfo = {
        unitGuid: this.changeschoolid,
        adjustState: this.adjustState,
        adjustTypeId: this.adjustTypeId
      }
      if (this.datetime != null) {
        this.searchInfo.startTime = this.datetime[0] + ' ' + this.time[0]
        this.searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
      }
      this.searchinit()
    },
    // 得到充值记录
    searchinit() {
      this.loading = true
      this.$http({
        url: this.$http.adornUrl('/card/recharge-type-balance-search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            ...this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        this.loading = false
        if (data.messageCode == 200) {
          this.tableinformation = data.messageContent || []
          if (this.tableinformation.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
            return
          }
          this.tableinformation.map(item => {
            item.sumRechargeAmount = this.amountyua(item.sumRechargeAmount)
          })
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    // 导出数据
    async exportInfo(type) {
      if (this.tableinformation.length == 0) {
        this.$message({
          message: '请查询数据后,在进行此操作',
          type: 'warning'
        })
        return false
      }
      if (type == 'print') {
        this.printJS()
      } else {
        this.onexport()
      }
    },

    // 拿到表头数据
    getIsPrint() {
      const tHeader = ['充值类型名称', '充值金额(元)']
      const filterValArr = ['adjustTypeName', 'sumRechargeAmount']
      return { filterValArr, tHeader }
    },
    // 打印
    printJS() {
      let someJSONdata = this.tableinformation
      const title = '无卡充值统计表'
      this.unitName = sessionStorage.getItem('unitName')
      const properties = []
      // 拿到数据,filterValArr为数据值，tHeader为数据标题，一一对应
      const { filterValArr, tHeader } = this.getIsPrint()
      filterValArr.map((item, index) => {
        properties.push({
          displayName: tHeader[index],
          field: filterValArr[index]
        })
      })
      // 打印
      this.$printJS({
        // 眉头
        documentTitle: `${this.unitName}`,
        /**
         * 获取表头的信息
         * @param {*} title 表名
         * @param {*} unitName 单位
         * @param {*} time 时间
         */
        header: this.printHeader(
          title,
          this.unitName,
          `${this.formatDate(
            new Date(this.searchInfo.startTime),
            'yyyy-MM-dd'
          )} 至 ${this.formatDate(
            new Date(this.searchInfo.endTime),
            'yyyy-MM-dd'
          )}`
        ),
        printable: someJSONdata,
        properties: properties,
        type: 'json',
        // 是否每页都显示头部
        // repeatTableHeader:false,
        headerRow: 1,
        /**
         * 获取页脚的信息
         * @param {*} loginName 制表人：
         * @param {*} time 时间
         */
        endPage: this.printFloor(
          sessionStorage.getItem('username'),
          this.getNowTime('yy-mm-dd hh:mm:ss')
        ),
        //  获取页脚的信息样式
        style: this.printFloorStyle()
      })
    },
    // 导出数据
    onexport() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        let multiHeader = []
        let merges = []
        const { tHeader, filterValArr } = this.getIsPrint()
        const { export_json_to_excel } = require('../../excel/Export2Excel')
        const filterVal = filterValArr
        const list = this.tableinformation
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader,
          header: tHeader,
          data,
          merges,
          filename: '无卡充值统计表' + this.getimeto()
          // tableheader: "1",
        })
      })
    },
    //分类页
    handleSizeChangepage(val) {
      this.pageSize = val
      this.searchinit()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.searchinit()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 280px;
}

.user_recharge_query {
  padding: 0 10px;
  .demo-form-inline1 {
    margin-top: 10px;
    /deep/.el-form-item {
      margin-bottom: 0px;
    }
  }
  //滚动条的宽度
  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  //滚动条的滑块
  ::-webkit-scrollbar-thumb {
    background-color: #a1a3a9;
    border-radius: 3px;
  }

  .page {
    // background-color: #f5f5f5;
    padding-bottom: 10px;
    .el-pagination {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
